package indi.yudidi;

import indi.yudidi.classloader.ClassTools;
import indi.yudidi.classloader.MyClassLoader;

import java.util.Iterator;
import java.util.Set;

public class TestMyClassLoader2 {
    public static void main(String[] args) throws Exception {
//        indi.yudidi.classloader.MyClassLoader mcl = new indi.yudidi.classloader.MyClassLoader();
        MyClassLoader mcl = new MyClassLoader(ClassLoader.getSystemClassLoader().getParent());
//        Class<?> c1 = Class.forName("indi.yudidi.Person", true, mcl);
        Class<?> c1 = readClassFileInClassPath("indi.yudidi.Person");
        Object obj = c1.newInstance();
        System.out.println(obj);
        System.out.println(obj.getClass());
        System.out.println(obj.getClass().getClassLoader());
    }

    private static Class readClassFileInClassPath(String classname) {
        Set<Class<?>> classes = ClassTools.getClasses("indi.yudidi");
        Iterator iterator = classes.iterator();
        while (iterator.hasNext()) {
            Class<?> c = (Class<?>) iterator.next();
            if (c.getName().equals(classname))
                return c;
        }
        return null;
    }
}